package com.chen.excel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ImageQueryService {

    @Autowired
    private ImageRecordRepository imageRecordRepository;

    public List<String> queryImageUrls(WaybillData waybillData) {
        List<String> imageUrls = new ArrayList<>();
        
        Optional<ImageRecord> exactMatch = findExactMatch(waybillData);
        if (exactMatch.isPresent()) {
            imageUrls.add(exactMatch.get().getImageUrl());
            return imageUrls;
        }
        
        List<ImageRecord> partialMatches = findPartialMatches(waybillData);
        for (ImageRecord record : partialMatches) {
            if (StringUtils.hasText(record.getImageUrl())) {
                imageUrls.add(record.getImageUrl());
            }
        }
        
        return imageUrls;
    }

    public List<WaybillImageResult> processWaybillDataList(List<WaybillData> waybillDataList) {
        List<WaybillImageResult> results = new ArrayList<>();
        
        for (WaybillData waybillData : waybillDataList) {
            List<String> imageUrls = queryImageUrls(waybillData);
            results.add(new WaybillImageResult(waybillData, imageUrls));
        }
        
        return results;
    }

    private Optional<ImageRecord> findExactMatch(WaybillData waybillData) {
        if (StringUtils.hasText(waybillData.getWaybillNo()) && 
            StringUtils.hasText(waybillData.getDriverName()) && 
            StringUtils.hasText(waybillData.getPlateNumber())) {
            
            return imageRecordRepository.findByWaybillNoAndDriverNameAndPlateNumber(
                waybillData.getWaybillNo(), 
                waybillData.getDriverName(), 
                waybillData.getPlateNumber()
            );
        }
        return Optional.empty();
    }

    private List<ImageRecord> findPartialMatches(WaybillData waybillData) {
        List<ImageRecord> matches = new ArrayList<>();
        
        if (StringUtils.hasText(waybillData.getWaybillNo())) {
            Optional<ImageRecord> record = imageRecordRepository.findByWaybillNo(waybillData.getWaybillNo());
            record.ifPresent(matches::add);
        }
        
        if (StringUtils.hasText(waybillData.getDriverName())) {
            List<ImageRecord> records = imageRecordRepository.findByDriverName(waybillData.getDriverName());
            for (ImageRecord record : records) {
                if (!matches.contains(record)) {
                    matches.add(record);
                }
            }
        }
        
        if (StringUtils.hasText(waybillData.getPlateNumber())) {
            List<ImageRecord> records = imageRecordRepository.findByPlateNumber(waybillData.getPlateNumber());
            for (ImageRecord record : records) {
                if (!matches.contains(record)) {
                    matches.add(record);
                }
            }
        }
        
        return matches;
    }

    public static class WaybillImageResult {
        private WaybillData waybillData;
        private List<String> imageUrls;

        public WaybillImageResult(WaybillData waybillData, List<String> imageUrls) {
            this.waybillData = waybillData;
            this.imageUrls = imageUrls;
        }

        public WaybillData getWaybillData() {
            return waybillData;
        }

        public void setWaybillData(WaybillData waybillData) {
            this.waybillData = waybillData;
        }

        public List<String> getImageUrls() {
            return imageUrls;
        }

        public void setImageUrls(List<String> imageUrls) {
            this.imageUrls = imageUrls;
        }

        @Override
        public String toString() {
            return "WaybillImageResult{" +
                    "waybillData=" + waybillData +
                    ", imageUrls=" + imageUrls +
                    '}';
        }
    }
}